package controllers

import (
	"fmt"
	"fxynewbee/models"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type MainController struct {
	beego.Controller
}

func (c *MainController) MyHome() {
	c.Data["mystr"] = "用户登录验证"
	c.TplName = "test.html"
}

func (c *MainController) MyorLogin() {
	getname := c.GetString("namehtml")
	getpwd := c.GetString("passhtml")
	if getname == "" || getpwd == "" {
		c.Ctx.Redirect(307, "/loginornot/errlogin")
	}
	fmt.Println("Loginusr:", getname)
	fmt.Println("Loginpwd:", getpwd)
	o := orm.NewOrm()
	finduser := models.User{}
	finduser.Username = getname

	err := o.Read(&finduser, "Username")
	if err != nil {
		c.Ctx.Redirect(307, "/loginornot/errlogin")
	}
	if finduser.Password == getpwd {
		c.Ctx.Redirect(307, "/loginornot/succlogin")
	} else {
		c.Ctx.Redirect(307, "/loginornot/errlogin")
	}
}

func (c *MainController) Loginsucc() {
	c.Data["mystr"] = "用户登陆成功"
	c.TplName = "test00.html"
}
func (c *MainController) Loginerr() {
	c.Data["mystr"] = "用户登录失败"
	c.TplName = "test00.html"
}
func (c *MainController) MyRegster() {
	c.Data["mystr"] = "用户注册"
	c.TplName = "test02.html"
}
func (c *MainController) Goback() {
	c.Ctx.Redirect(302, "/")
}
func (c *MainController) Myorregs() {
	getname := c.GetString("usernamehtml")
	getpwd := c.GetString("numberhtml")
	if getname == "" || getpwd == "" {
		c.Ctx.Redirect(307, "/myregster/regsornot/errregs")
		return
	}
	o := orm.NewOrm()
	insertuser := models.User{}
	insertuser.Username = getname
	insertuser.Password = getpwd
	_, err := o.Insert(&insertuser)
	if err != nil {
		fmt.Println("Err:", err)
		c.Ctx.Redirect(307, "/myregster/regsornot/errregs")
	} else {
		fmt.Println("Regsusr:", getname)
		fmt.Println("Regspwd:", getpwd)
		c.Ctx.Redirect(307, "/myregster/regsornot/succregs")
	}
}
func (c *MainController) Regserr() {
	c.Data["mystr"] = "用户注册失败"
	c.TplName = "test00.html"
}
func (c *MainController) Regssucc() {
	c.Data["mystr"] = "用户注册成功"
	c.TplName = "test00.html"
}
